> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mlfoundations/open_clip/llms.txt
> Use this file to discover all available pages before exploring further.

# PreprocessCfg

> Configuration dataclass for image preprocessing

## Overview

`PreprocessCfg` is a dataclass that encapsulates all preprocessing parameters for CLIP image transforms. It provides type-safe configuration for image resizing, normalization, and color mode settings.

## Class Definition

```python theme={null}
@dataclass
class PreprocessCfg:
    size: Union[int, Tuple[int, int]] = 224
    mode: str = 'RGB'
    mean: Tuple[float, ...] = OPENAI_DATASET_MEAN
    std: Tuple[float, ...] = OPENAI_DATASET_STD
    interpolation: str = 'bicubic'
    resize_mode: str = 'shortest'
    fill_color: int = 0
```

## Fields

<ParamField path="size" type="Union[int, Tuple[int, int]]" default="224">
  Target size for preprocessed images.

  * **int**: Square images (e.g., `224` → 224×224)
  * **Tuple\[int, int]**: Rectangular images `(height, width)` (e.g., `(384, 224)`)

  Common sizes:

  * `224`: ViT-B, ViT-L (base resolution)
  * `336`: ViT-L (high resolution)
  * `384`: ViT-L/14\@336px
</ParamField>

<ParamField path="mode" type="str" default="'RGB'">
  Color mode for image conversion. Currently only `'RGB'` is supported.

  Images are automatically converted to RGB with 3 channels.
</ParamField>

<ParamField path="mean" type="Tuple[float, ...]" default="(0.48145466, 0.4578275, 0.40821073)">
  Mean values for normalization, one per channel (R, G, B).

  Default values are from the OPENAI CLIP dataset:

  * R: 0.48145466
  * G: 0.4578275
  * B: 0.40821073

  Used in: `normalized = (image - mean) / std`
</ParamField>

<ParamField path="std" type="Tuple[float, ...]" default="(0.26862954, 0.26130258, 0.27577711)">
  Standard deviation values for normalization, one per channel (R, G, B).

  Default values are from the OPENAI CLIP dataset:

  * R: 0.26862954
  * G: 0.26130258
  * B: 0.27577711

  Used in: `normalized = (image - mean) / std`
</ParamField>

<ParamField path="interpolation" type="str" default="'bicubic'">
  Interpolation method for resizing images.

  Options:

  * `'bicubic'`: High-quality interpolation (recommended, default for CLIP)
  * `'bilinear'`: Faster but lower quality
  * `'random'`: Randomly choose between bicubic/bilinear (training only)
</ParamField>

<ParamField path="resize_mode" type="str" default="'shortest'">
  Strategy for resizing images to target size.

  Options:

  * `'shortest'`: Resize shortest edge to target size, then center crop
  * `'longest'`: Resize longest edge to target size, then pad and center crop
  * `'squash'`: Resize to exact target size (may distort aspect ratio)
</ParamField>

<ParamField path="fill_color" type="int" default="0">
  Fill color value (0-255) for padding when using `resize_mode='longest'`.

  * `0`: Black padding (default)
  * `255`: White padding
  * Other values: Gray shades
</ParamField>

## Properties

### num\_channels

```python theme={null}
@property
def num_channels(self) -> int:
    return 3
```

Returns the number of image channels (always 3 for RGB).

### input\_size

```python theme={null}
@property
def input_size(self) -> Tuple[int, int, int]:
    return (self.num_channels,) + to_2tuple(self.size)
```

Returns the expected input tensor shape: `(channels, height, width)`.

## Examples

### Create default config

```python theme={null}
from open_clip import PreprocessCfg

# Use all defaults (224x224, ImageNet normalization)
cfg = PreprocessCfg()
print(cfg.input_size)  # (3, 224, 224)
```

### Custom image size

```python theme={null}
# Square images
cfg_224 = PreprocessCfg(size=224)
cfg_336 = PreprocessCfg(size=336)

# Rectangular images
cfg_rect = PreprocessCfg(size=(384, 224))  # height, width
print(cfg_rect.input_size)  # (3, 384, 224)
```

### Custom normalization

```python theme={null}
# Use ImageNet statistics instead of CLIP
from torchvision.transforms import Normalize

cfg = PreprocessCfg(
    size=224,
    mean=(0.485, 0.456, 0.406),
    std=(0.229, 0.224, 0.225)
)
```

### Different resize modes

```python theme={null}
# Shortest edge resize (default)
cfg_shortest = PreprocessCfg(size=224, resize_mode='shortest')

# Longest edge with padding
cfg_longest = PreprocessCfg(
    size=224,
    resize_mode='longest',
    fill_color=128  # Gray padding
)

# Squash to exact size
cfg_squash = PreprocessCfg(size=224, resize_mode='squash')
```

### High-resolution config

```python theme={null}
# ViT-L-14@336px configuration
cfg_high_res = PreprocessCfg(
    size=336,
    interpolation='bicubic',
    resize_mode='shortest'
)
```

### Use with image\_transform\_v2

```python theme={null}
import open_clip
from PIL import Image

# Create config
preprocess_cfg = open_clip.PreprocessCfg(
    size=224,
    interpolation='bicubic'
)

# Create transform
transform = open_clip.image_transform_v2(
    cfg=preprocess_cfg,
    is_train=False
)

# Apply to image
image = Image.open('photo.jpg')
tensor = transform(image)
print(tensor.shape)  # torch.Size([3, 224, 224])
```

### Extract from model

```python theme={null}
import open_clip

# Get preprocessing config from a model
model, preprocess_cfg, _ = open_clip.create_model_and_transforms(
    'ViT-B-32',
    pretrained='laion2b_s34b_b79k'
)

print(f"Size: {preprocess_cfg.size}")
print(f"Mean: {preprocess_cfg.mean}")
print(f"Std: {preprocess_cfg.std}")
```

### Merge configs

```python theme={null}
from open_clip.transform import merge_preprocess_dict

# Start with base config
base_cfg = PreprocessCfg(size=224)

# Override specific fields
overlay = {'size': 336, 'interpolation': 'bilinear'}
merged = merge_preprocess_dict(base_cfg, overlay)

# Create new config from merged dict
new_cfg = PreprocessCfg(**merged)
```

## Resize Mode Comparison

| Mode       | Behavior                           | Use Case                            |
| ---------- | ---------------------------------- | ----------------------------------- |
| `shortest` | Resize shortest edge, crop center  | **Default**, preserves aspect ratio |
| `longest`  | Resize longest edge, pad to square | When you want to see entire image   |
| `squash`   | Resize to exact dimensions         | May distort, fastest                |

## Common Configurations

<CardGroup cols={2}>
  <Card title="ViT-B/32" icon="image">
    ```python theme={null}
    PreprocessCfg(
        size=224,
        interpolation='bicubic'
    )
    ```
  </Card>

  <Card title="ViT-L/14" icon="image">
    ```python theme={null}
    PreprocessCfg(
        size=224,
        interpolation='bicubic'
    )
    ```
  </Card>

  <Card title="ViT-L/14@336" icon="expand">
    ```python theme={null}
    PreprocessCfg(
        size=336,
        interpolation='bicubic'
    )
    ```
  </Card>

  <Card title="ViT-H/14" icon="image">
    ```python theme={null}
    PreprocessCfg(
        size=224,
        interpolation='bicubic'
    )
    ```
  </Card>
</CardGroup>

## Notes

* Always use `PreprocessCfg` instead of passing individual parameters to transforms
* The config object is returned by [`create_model_and_transforms()`](/api/create-model-and-transforms)
* Mean and std values should match the model's training data statistics
* Use `bicubic` interpolation for best quality (matches CLIP training)
* `resize_mode='shortest'` is the standard CLIP preprocessing approach

## See Also

* [`AugmentationCfg`](/api/augmentation-cfg) - Augmentation configuration for training
* [`image_transform_v2()`](/api/image-transform) - Create transforms from config
* [`create_model_and_transforms()`](/api/create-model-and-transforms) - Get model with config
